<script>
import addAddress from "@/components/addAddress.vue";
import updateAddr from "@/components/updateAddr.vue";
import axios from "axios";

export default {
  components: {updateAddr, addAddress},
  props: ["props"],
  data() {
    return {
      state: {
        updateForm: "",
        userId: ""
      },
      updateFormVisible: false,
      addFormVisible: false,
    }
  }, methods: {
    align() {
      return "text-align:center"
    },
    openEditDialog(item) {
      this.state.updateForm = JSON.parse(JSON.stringify(item));
      this.updateFormVisible = true;
    },
    openAddDialog(userId) {
      this.state.userId = userId
      this.addFormVisible = true;
    },
    myAddrVisible(val) {
      this.addFormVisible = val
      this.updateFormVisible = val
    },
    deleteAddress(item) {
      this.$confirm('此操作将永久删除该地址, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$api.address.deleteAddressById(item.id).then((res) => {
          var data = res.data;
          if (data.success) {
            this.$message.success(data.message)
            this.state.page = 1;
            this.$emit('changeVal', true)
          } else {
            this.$message.error(data.message);
          }
        }).catch((resp) => {
          this.$message.error('服务器访问失败' + resp);
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
  },watch:{
    updateFormVisible(){
      this.$emit('changeVal', !this.updateFormVisible)
    },
    addFormVisible(){
      this.$emit('changeVal', !this.addFormVisible)
    }
  }
}

</script>

<template>
  <div>
    <el-button size="mini" type="success" @click="openAddDialog(props[0].userId)">新增地址</el-button>
    <el-table
        :data="props"
        border
        :cell-style="align"
        style="width: 100%">
      <el-table-column
          prop="id"
          align="center"
          label="编号">
      </el-table-column>
      <el-table-column
          prop="name"
          align="center"
          label="收货人">
      </el-table-column>
      <el-table-column
          prop="address"
          align="center"
          label="收货地址">
      </el-table-column>
      <el-table-column
          prop="phone"
          align="center"
          label="联系方式">
      </el-table-column>
      <el-table-column
          prop="tag"
          align="center"
          label="标签">
      </el-table-column>
      <el-table-column
          fixed="right"
          align="center"
          label="操作"
          width="150">
        <template slot-scope="scope">
          <el-button link type="success" size="mini" @click="openEditDialog(scope.row)">编辑</el-button>
          <el-button link type="danger" size="mini" @click="deleteAddress(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <addAddress v-if="addFormVisible" @changeVal="myAddrVisible" :userId="state.userId"></addAddress>
    <updateAddr v-if="updateFormVisible" @changeVal="myAddrVisible" :updateForm="state.updateForm"></updateAddr>
  </div>

</template>

<style scoped>

</style>